﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.BLL;
using MyOA.Model;
namespace MyOA.UI.ScheduleManage.PersonNote
{
    public partial class AddNode : System.Web.UI.Page
    {
        AddNoteManage manage = new AddNoteManage();
        
        
        /// <summary>
        /// 构建实体
        /// </summary>
        /// <returns>返回便签实体</returns>
        protected MyNote createMyNote()
        {
            MyNote note=new MyNote();
            note.NoteContent=txtNoteContent.Text;
            note.NoteTitle = txtTitle.Text;
            if (lblCreateTime.Text.Trim() == string.Empty)
            {
                note.CreateTime = DateTime.Now;
            }
            else
            {
                note.CreateTime = DateTime.Parse(lblCreateTime.Text);
            }
            note.CreateUser.UserId = manage.getUserIdByLoginId(Session["loginId"].ToString());
            return note;
        }
       
        /// <summary>
        /// 为控件赋值便签信息
        /// </summary>
        /// <param name="note">传入的便签实体</param>
        protected void dataBind(MyNote note)
        {
            txtNoteContent.Text = note.NoteContent;
            txtTitle.Text = note.NoteTitle;
            
            lblCreateTime.Text = note.CreateTime.ToString();
       }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;

                }
              
                string loginId = (string)Session["loginId"];
               
                //string loginId = (string)Session["loginId"];//获取登陆号
                lblCreateUser.Text = (string)Session["loginId"];

                if (Request.QueryString["noteId"] != null)
                {
                    int noteId = int.Parse(Request.QueryString["noteId"]);
                    MyNote note = manage.getMyNoteByNoteId(noteId);//获得便签信息
                    dataBind(note);//数据绑定


                }
                else
                {

                    btnDelete.Enabled = false;
                }
                
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string message = string.Empty;
            int noteId = -1;
            MyNote note = createMyNote();
            
            if (Request.QueryString["noteId"]==null)//没传入值 说明是新增便签
            {
                
                if (manage.insertMyNote(note)>0)
                {
                    message = "增加便签成功";
                }
                else
                {
                    message = "增加便签失败";
                }
            }
            else//传入值 说明是修改便签
            {
                noteId = int.Parse(Request.QueryString["noteId"]);
                note.NoteId = noteId;
                if (manage.UpdateMyNoteByNoteId(note)>0)
                {
                    message = "修改便签成功";
                }
                else
                {
                    message = "修改便签失败";
                }
            }

            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
           
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string message = string.Empty;
            int noteId = int.Parse(Request.QueryString["noteId"]);
            if (manage.deleteMyNoteByNoteId(noteId)>0)
            {
                message = "删除便签成功";
            }
            else
            {
                message = "删除便签失败";
            }


            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
        }
    }
}
